let picChart;
let colors;
function initEchart(){
    picChart = echarts.init(document.getElementById("pic"));
    colors = ['#5470C6', '#91CC75', '#EE6666'];
}
function initPoetryAuthorType(res) {
    picChart.clear();
    let poetry = res.maxPoetry;
    let author = res.maxAuthor;
    let type = res.maxType;
    poetry += Math.ceil(poetry * 0.2);
    author += Math.ceil(author * 0.2);
    type += Math.ceil(type * 0.2);
    let option = {
        color: colors,
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                crossStyle: {
                    color: '#999'
                }
            }
        },
        grid: {
            right: '20%'
        },
        toolbox: {
            feature: {
                dataView: {show: true, readOnly: false},
                magicType: {show: true, type: ['line', 'bar']},
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        legend: {
            data: ['作者', '作品', '类型']
        },
        xAxis: [
            {
                type: 'category',
                data: res.dynastyRes,
                axisTick: {
                    alignWithLabel: true
                },
                axisPointer: {
                    type: 'shadow'
                }
            }
        ],
        yAxis: [
            {
                type: 'value',
                name: '作者',
                min: 0,
                max: author,
                interval: Math.ceil(author / 4),
                position: 'right',
                axisLabel: {
                    formatter: '{value} 人'
                },
                axisLine: {
                    show: true,
                    lineStyle: {
                        color: colors[0]
                    }
                }
            },
            {
                type: 'value',
                name: '作品',
                min: 0,
                max: poetry,
                interval: Math.ceil(poetry / 4),
                position: 'right',
                offset: 80,
                axisLine: {
                    show: true,
                    lineStyle: {
                        color: colors[1]
                    }
                },
                axisLabel: {
                    formatter: '{value} 种'
                }
            },
            {
                type: 'value',
                name: '类型',
                min: 0,
                max: type,
                interval: Math.ceil(type / 4),
                position: 'left',
                axisLine: {
                    show: true,
                    lineStyle: {
                        color: colors[2]
                    }
                },
                axisLabel: {
                    formatter: '{value} 种'
                }
            }
        ],
        series: [
            {
                name: '作者',
                type: 'bar',
                data: res.authorRes
            },
            {
                name: '作品',
                type: 'bar',
                yAxisIndex: 1,
                data: res.poetryRes
            },
            {
                name: '类型',
                type: 'line',
                yAxisIndex: 2,
                data: res.typeRes
            }
        ]
    };
    picChart.setOption(option);
}
function initAuthorPoetry(res){
    picChart.clear();
    let option = {
        title: {
            text: '作者统计',
            subtext: '统计作者对应的作品数量',
            left: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
            type: 'scroll',
            orient: 'vertical',
            right: 400,
            top: 20,
            bottom: 20,
            data: res.names
        },
        series: [
            {
                name: '姓名',
                type: 'pie',
                radius: '55%',
                center: ['50%', '50%'],
                data: res.list,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    picChart.setOption(option);
}
function statisticsAuthorPoetry(){
    $.ajax({
        url: "/statistics/statisticsAuthorPoetry",
        dataType: "json",
        type: "GET",
        success: function (json) {
            if(json.code === 200){
                initAuthorPoetry(json.data);
            }else{
                tipsMessage(json);
            }
        }
    });
}
function poetryAuthorType() {
    $.ajax({
        url: "/statistics/statisticsResult",
        dataType: "json",
        type: "GET",
        success: function (json) {
            if(json.code === 200){
                initPoetryAuthorType(json.data);
            }else{
                tipsMessage(json);
            }
        }
    });
}
window.onload = () => {
    setBackPageIsMainPageFlag();
    initEchart();
    poetryAuthorType();
}